package day01.text02;

import java.util.concurrent.atomic.AtomicInteger;

public class Seller implements Runnable {


 private  Integer stock=100;
    private static Object obj = new Object();

    AtomicInteger at = new AtomicInteger(100);


    public Seller(){

    }

  public Seller(int stock) {
        this.stock = stock;
    }


    @Override
    public void run() {
        //使用锁方法防止出现多个线程卖同一个商品的情况
        //当stock不等于0的时候结束循环
        while (true) {
                if(at.get()<0){
                    break;
                }else {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    stock--;
                    System.out.println(Thread.currentThread().getName()+"卖掉了"+stock+"号商品");
                }
                }
            }
        }


